package com.diamond.webservice.util;

import org.springframework.util.StringUtils;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 加密工具类
 */
public class EncryptionUtil {
    /**
     *获取Base64编码字符串
     * @param imgFile
     * @return
     */
    public static String getBase64Str(String imgFile) {
        if (StringUtils.isEmpty(imgFile)){
            return null;
        }
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            int read = inputStream.read(data);
            // 加密
            if(data !=null) {
                BASE64Encoder encoder = new BASE64Encoder();
                return encoder.encode(data);
            }
        } catch (IOException e) {
            return null;
//            e.printStackTrace();
        }finally {
            if(inputStream!=null) {
                try{
                    inputStream.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
